<?php
namespace app\admin\controller;

use app\admin\model\Website as websiteModel;
use app\admin\validate\WebsiteValidate;
use tool\Log;

class Website extends Base
{
    public function index()
    {
        if (request()->isAjax()) {
            $limit = input('param.limit');
            $keywords = input('param.keywords');
            $where = [];
            if (!empty($keywords)) {
                $where[] = ['weburl', 'like', '%' . $keywords . '%'];
            }

            $web = new websiteModel();
            $list = $web->getWeb($limit, $where);
            if (0 == $list['code']) {
                return json(['code' => 0, 'msg' => 'ok', 'count' => $list['data']->total(), 'data' => $list['data']->all()]);
            }
            return json(['code' => 0, 'msg' => 'ok', 'count' => 0, 'data' => []]);
        }
        return $this->fetch();
    }
    // 添加网站
    public function add()
    {
        if (request()->isPost()) {

            $param = input('post.');
            if(strpos($param['weburl'],'://') !== false) {
                return reMsg(-1,'','网站URL不需要http://,请修改后再提交');
            }

            $validate = new WebsiteValidate();
            if (!$validate->check($param)) {
                return json(['code' => -1, 'data' => '', 'msg' => $validate->getError()]);
            }

            $web = new websiteModel();
            $res = $web->addWeb($param);

            Log::write("添加网站：" . $param['weburl']);

            return json($res);
        }

        return $this->fetch();
    }
    // 编辑网站
    public function edit()
    {
        if (request()->isPost()) {

            $param = input('post.');

            $validate = new WebsiteValidate();
            if (!$validate->scene('edit')->check($param)) {
                return json(['code' => -1, 'data' => '', 'msg' => $validate->getError()]);
            }

            $web = new websiteModel();
            $res = $web->editWeb($param);

            Log::write("编辑网站：" . $param['weburl']);

            return json($res);
        }

        $id = input('param.id');
        $web = new websiteModel();

        $this->assign([
            'detail' => $web->getWebsiteById($id)['data'],
        ]);

        return $this->fetch('edit');
    }
    // 修改单个网站的备注信息
    public function editDesc()
    {
        if (request()->isAjax()) {
            $param['id'] = input('param.id');
            $param['desc'] = input('param.desc');
            $web = new websiteModel();
            if (false !== $web->save($param, ['id' => $param['id']])) {
                return reMsg(0, '', '备注修改成功');
            } else {
                return reMsg(-1, '', '备注修改失败');
            }
        }
    }
    /**
     * 删除订单
     * @return \think\response\Json
     */
    public function del()
    {
        if (request()->isAjax()) {
            $id = input('param.id');

            $web = new websiteModel();
            $res = $web->delWeb($id);

            Log::write("删除网站：" . $id);
            return json($res);
        }
    }

}